package cvar.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LPModelBuilder {

  private final List<Constraint> eqs_;
  private final List<Constraint> ieqs_;
  private final Map<Variable, Double> variables_;
  
  public LPModelBuilder() {
    variables_ = new HashMap<Variable, Double>();
    eqs_ = new ArrayList<Constraint>();
    ieqs_ = new ArrayList<Constraint>();
  }
  
  public LPModel build() {
    Variable[] vars = new Variable[variables_.size()];
    double[] weights = new double[variables_.size()];
    HashMap<Variable, Integer> index = new HashMap<Variable, Integer>();
    int i = 0;
    for (Variable x : variables_.keySet()) {
      vars[i] = x;
      weights[i] = variables_.get(x);
      index.put(x, i);
      ++i;
    }
    return new LPModel(vars, weights, ieqs_, eqs_, index);
  }

  public Variable addNonNegativeVariable(String name) {
    Variable var = new Variable(name);
    variables_.put(var, 0.0);
    return var;
  }

  public Constraint addInequality() {
    Constraint ieq = new Constraint();
    ieqs_.add(ieq);
    return ieq;
  }
  
  public Constraint addEquality() {
    Constraint eq = new Constraint();
    eqs_.add(eq);
    return eq;
  }

  public void setVariableWeight(Variable var, double weight) {
    variables_.put(var, weight);
  }

}
